package com.sc.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sc.springboot.pojo.FavorRecord;
import com.sc.springboot.pojo.Users;
import com.sc.springboot.service.IFavorRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author romeo
 * @since 2025-09-22 07:55:43
 */
@RestController
@RequestMapping("/favorRecord")
//偏向记录
public class FavorRecordController {
    @Autowired
    private IFavorRecordService favorRecordService;


    //偏向记录添加方法
    public void add(Integer userId,Integer typeId,Integer score){
        //查询表中是否有该用户对该类型资源的偏向记录
        QueryWrapper<FavorRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("type_id",typeId);
        FavorRecord favorRecord = favorRecordService.getOne(queryWrapper);
        if (favorRecord != null){   //存在则更新数据
            favorRecord.setFavorScore(favorRecord.getFavorScore()+score);
            favorRecordService.update(favorRecord,queryWrapper);
        }else {
            FavorRecord favorRecordx = new FavorRecord();
            favorRecordx.setUserId(userId);
            favorRecordx.setTypeId(typeId);
            favorRecordx.setFavorScore(1);
            favorRecordService.save(favorRecordx);
        }
    }




}
